/**
 * 
 */
package net.sahv.bdyz.controller.web;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.model.ShowSynchronization;
import net.sahv.bdyz.model.ShufflingFigure;
import net.sahv.bdyz.model.Theme;
import net.sahv.bdyz.service.FabricService;
import net.sahv.bdyz.service.LanguageService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.service.PatternService;
import net.sahv.bdyz.service.ShowSynchronizationService;
import net.sahv.bdyz.service.ShufflingFigureService;
import net.sahv.bdyz.service.ThemeService;
import net.sahv.bdyz.util.LoadCfgUtil;
import net.sahv.bdyz.util.PageInfo;

/**
 * @author 张天敏
 * @date 日期：2016年8月3日 时间：下午4:04:52
 */
@Controller
@RequestMapping("web/main")
public class MainController {
	
	@Autowired 
	CookieLocaleResolver resolver;
	@Autowired
	private ShowSynchronizationService showSynchronizationService;// 秀场
	@Autowired
	private FabricService fabricService;// 面料
	@Autowired
	private PatternService patternService;// 花型
	@Autowired
	private MemberService memberService;// 会员
	@Autowired
	private ThemeService themeService;// 主题
	@Autowired
	private ShufflingFigureService shufflingFigureService;
	@Autowired
	private LanguageService languageService;
	
	/**
	 * 跳转到首页
	 * 
	 * @author 张天敏
	 * @date 日期：2016年8月3日 时间：下午4:05:47
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model)
			throws FileNotFoundException, IOException {
		Map<String, String> mapUrls = LoadCfgUtil.loadCfg();
		String imgParent = mapUrls.get("targetUrl.img");
		String language = languageService.getCurrentLanguage();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageSize(9);
		pageInfo.setCount(0);
		String bbWhere = " F_IS_DELETE=0 and F_TYPE=2";// 查询版布开发条件
		String ybWhere = " F_IS_DELETE=0 and F_TYPE=3";// 查询样本开发条件
		String where = " p.P_IS_DELETE=0 and p.P_SEND_STATE=1 and p.P_BUYOUT_STATE=0";// 花型
		String order = " order by p.P_SEND_TIME desc";// 花型
		String orderStr = " order by F_SEND_TIME desc";// 面料
		Member member = memberService.getCurrent();
		int mId = 0;
		if (member != null) {
			mId = member.getmId();
		}
		List<ShowSynchronization> showList = showSynchronizationService.selectAll();// 全部秀场同步
		List<Map<String, Object>> bbList = fabricService.getByPage(pageInfo, bbWhere, orderStr, mId);// 版布
		List<Map<String, Object>> ybList = fabricService.getByPage(pageInfo, ybWhere, orderStr, mId);// 样本
		// 花型列表 分页
		List<Map<String, Object>> pList = patternService.getByPage(pageInfo, where, order, mId);
		List<Theme> themeList = themeService.findThemeList();// 主题
		if (language.equals("en")) {
			for (Theme th : themeList) {
				th.setName(th.getNameEn());
			}
			for (ShowSynchronization ss : showList) {
				ss.setName(ss.getEnName());
			}
			for (Map<String, Object> map : ybList) {
				map.put("fName", map.get("fNameEn"));
			}
		}
		//首页图片列表
		List<ShufflingFigure> shufflingFigureList=shufflingFigureService.selectShufflingFigureList();
		model.addAttribute("imgpath", imgParent);
		model.addAttribute("showList", showList);
		model.addAttribute("bbList", bbList);
		model.addAttribute("ybList", ybList);
		model.addAttribute("pList", pList);
		model.addAttribute("member", member);
		model.addAttribute("themeList", themeList);
		model.addAttribute("shufflingFigure", shufflingFigureList);
		return "web/main/index";
	}
	
	/**
	 * 更改默认的显示语言
	 * @author 张天敏
	 * @date 日期：2016年8月8日 时间：下午2:25:38
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="changeLanguage",method=RequestMethod.POST)
	public ApiResult changeLanguage(HttpServletRequest request)
	{
		ApiResult result=new ApiResult();
		String language=(String) request.getParameter("language");
		HttpSession session=request.getSession();
		Member member=(Member) session.getAttribute("member");
		if(language!=null&&language.equals("en")){
			session.setAttribute("language", "en");
			if(member!=null){
				//获取登陆用户的主键Id
				Integer mId=member.getmId();
				Member member2=memberService.selectByPrimaryKey(mId);
				member2.setmLanguage(2);
				memberService.updateByPrimaryKey(member2);
				session.removeAttribute("member");
				session.setAttribute("member", member);
			}
		}else if(language!=null&&language.equals("zh_cn")){
			session.setAttribute("language", "zh_cn");
			if(member!=null){
				//获取登陆用户的主键Id
				Integer mId=member.getmId();
				Member member2=memberService.selectByPrimaryKey(mId);
				member2.setmLanguage(1);
				memberService.updateByPrimaryKey(member2);
				session.removeAttribute("member");
				session.setAttribute("member", member);
			}
		}
		result.msg.status=0;
		result.msg.desc="语言切换成功！";
		return result;
	}
	
	
	
	

}
